"""
Case Type   : 服务端工具
Case Name   : 验证gs_encrypt工具功能
Create At   : 2024.10.8
Owner       : wang-tianjing1
Description :
    1.查看帮助信息
    2.查看版本信息
    3.使用参数-k,-v加密铭文字符串
    4.在$GAUSSHOME/bin目录下生成加密文件cipher,rand
Expect      :
    1.显示详细的帮助信息
    2.显示工具版本信息
    3.成功
    4.成功生成文件
History     :
"""

import unittest
import os 
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.dbuserNode = Node('PrimaryDbUser')
        self.str = 'test'
        self.encrypt = '52$$KKll'
        self.vec = '1234567891236548'
        self.prefix = 'cjq'
        self.encrypt = '125#DDdd'
        self.file_path = os.path.join(os.path.dirname(macro.DB_INSTANCE_PATH),
                                     'app', 'bin')
        self.cipher_path = os.path.join(self.file_path, f'{self.prefix}.key.'
                                                        f'cipher')
        self.rand_path = os.path.join(self.file_path, f'{self.prefix}.key.'
                                                      f'rand')

    def test_gs_encrypt(self):
        step = '-----step1:查看帮助信息  except:显示详细的帮助信息-----'
        self.log.info(step)
        check_cmd1 = f'''source {macro.DB_ENV_PATH};
            gs_encrypt -? '''
        self.log.info(check_cmd1)
        msg1 = self.dbuserNode.sh(check_cmd1).result()
        self.log.info(msg1)
        self.log.info('-----截取General options部分的语法-----')
        start_index1 = msg1.find('General options:')
        self.log.info('定义一个空的列表，将获取的参数写入，打印出该列表')
        options_list1 = []
        for i in msg1[start_index1:].splitlines():
            for j in i.split(' '):
                if len(j) == 2 and j[0] == '-':
                    options_list1.append(j)
        self.log.info('-----打印列表信息-----')
        self.log.info(options_list1)
        parameter = ['-?', '-V', '-k', '-v,', '-f', '-B', '-D']
        self.assertEqual(options_list1.sort(), parameter.sort(),
                         "执行失败" + step)

        step = '----step2:查看版本信息  except:显示工具版本信息-----'
        self.log.info(step)
        check_cmd1 = f'''source {macro.DB_ENV_PATH};
                    gs_encrypt -V '''
        self.log.info(check_cmd1)
        msg1 = self.dbuserNode.sh(check_cmd1).result()
        self.log.info("-----版本信息-----")
        self.log.info(msg1)
        self.assertIn('openGauss', msg1, "执行失败" + step)

        step = '-----step3:使用参数-k,-v加密铭文字符串;except:成功-----'
        self.log.info(step)
        check_cmd1 = f'''source {macro.DB_ENV_PATH};
                    gs_encrypt -k {self.encrypt} -v {self.vec} {self.str}'''
        self.log.info(check_cmd1)
        msg1 = self.dbuserNode.sh(check_cmd1).result()
        self.log.info("-----加密结果-----")
        self.log.info(msg1)
        self.assertEqual(len(msg1), 64, "执行失败" + step)

        step = "------step4:在$GAUSSHOME/bin目录下生成加密文件cipher,rand；except:成功生成文件"
        self.log.info(step)
        check_cmd1 = f''' source {macro.DB_ENV_PATH};
                    gs_guc generate -S {self.encrypt} -D {self.file_path} -o \
                    {self.prefix};
                    ls {self.file_path}'''
        self.log.info(check_cmd1)
        msg1 = self.dbuserNode.sh(check_cmd1).result()
        self.log.info(msg1)
        self.assertTrue(f'{self.prefix}.key.cipher' in msg1
                        and f'{self.prefix}.key.rand' in msg1,
                        "生成失败" + step)

    def tearDown(self):
        self.log.info('-----无需清理环境-----')
        self.log.info(f'-----{os.path.basename(__file__)} end------')
